package servlet.ann.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 全局异常处理
 */
@WebServlet(urlPatterns = "/errorHandler")
public class ErrorHandlerServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet_name:" + req.getAttribute("javax.servlet.error.servlet_name"));
        System.out.println("status_code:" + req.getAttribute("javax.servlet.error.status_code"));
        System.out.println("exception_type:" + req.getAttribute("javax.servlet.error.exception_type"));
        System.out.println("exception:" + req.getAttribute("javax.servlet.error.exception"));
        System.out.println("message:" + req.getAttribute("javax.servlet.error.message"));
        System.out.println("request_uri:" + req.getAttribute("javax.servlet.error.request_uri"));

        // 转发，页面网址不跳转
        req.getRequestDispatcher("WEB-INF/error.html").forward(req, resp);

        // 重定向
        // response.sendRedirect("WEB-INF/error.html");
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}